package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ProgressBar;
import android.os.Handler;
import android.os.Looper;

public class MainActivity extends AppCompatActivity {


    private CheckBox checkBoxAgreement;
    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    private ProgressBar progressLoading;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化Handler，关联主线程的Looper
        handler = new Handler(Looper.getMainLooper());

        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        checkBoxAgreement = findViewById(R.id.checkbox_agreement);
        btnLogin = findViewById(R.id.btn_login);
        progressLoading = findViewById(R.id.progress_loading);

        // 复选框状态变化监听
        checkBoxAgreement.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(MainActivity.this, "已勾选同意协议", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "已取消勾选同意协议", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 登录按钮点击事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();

                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (!checkBoxAgreement.isChecked()) {
                    Toast.makeText(MainActivity.this, "请先勾选同意用户协议", Toast.LENGTH_SHORT).show();
                } else {
                    progressLoading.setVisibility(View.VISIBLE);
                    btnLogin.setEnabled(false);
                    etUsername.setEnabled(false);
                    etPassword.setEnabled(false);
                    checkBoxAgreement.setEnabled(false);
                    handleLogin(username, password);
                }

            }
        });
    }

    private void handleLogin(final String username, final String password) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);

                    final boolean loginSuccess = "admin".equals(username) && "123456".equals(password);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            progressLoading.setVisibility(View.GONE);
                            btnLogin.setEnabled(true);
                            etUsername.setEnabled(true);
                            etPassword.setEnabled(true);
                            checkBoxAgreement.setEnabled(true);

                            if (loginSuccess) {
                                Toast.makeText(MainActivity.this, "登录成功！", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(MainActivity.this, "登录失败，用户名或密码错误", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            progressLoading.setVisibility(View.GONE);
                            btnLogin.setEnabled(true);
                            etUsername.setEnabled(true);
                            etPassword.setEnabled(true);
                            checkBoxAgreement.setEnabled(true);
                            Toast.makeText(MainActivity.this, "登录过程被中断", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }
}
